package com.peanut.controller;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @className: com.peanut.controller-> ScopConroller
 * @description: 这个类是做做什么得吖，小朋友？
 * @author: chenhs
 * @createDate: 2021-08-09 21:53
 * @version: 1.0
 */
@Controller
public class ScopConroller {
    //servletAPI共享数据
    @RequestMapping(value = "/scop1")
    public String name(HttpServletRequest request) {
        request.setAttribute("testRequsetScope", "servletAPI");
        return "success";
    }

    // ModelAndView使用  ModelAndView共享数据
    //    如果使用了  ModelAndView返回值必须是  ModelAndView。
    @RequestMapping(value = "/scop2")
    public ModelAndView name2() {
        ModelAndView view = new ModelAndView();
        //添加数据
        view.addObject("testRequsetScope", " 你好,ModelAndView");
        //设置视图名称
        view.setViewName("success");
        return view;
    }

    /**
     * 使用 Mode共享数据
     *
     * */
    @RequestMapping(value = "/testModel")
    public String testMode(Model model) {
        model.addAttribute("testRequsetScope","你好 ， model");
        return "success";
    }
//map 共享数据
    @RequestMapping(value = "/testMap")
    public String testMap(Map<String,Object> map) {
        map.put("testRequsetScope","你好 map");
        return "success";
    }
  // 使用 ModelMap  共享数据
    @RequestMapping(value = "/testModelMap")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("testRequsetScope","你好 ModelMap");
        return "success";
    }

    @RequestMapping(value = "/testForward")
    public String testForward() {
//        forward: 表示转发
        return "forward:/scop1";
    }
    @RequestMapping(value = "/testRedirect")
    public String testRedirect() {
//         redirect:表示重定向
        return "redirect:/scop1";
    }
}
